﻿<?php 
include("../safe.php");
include("function.php");
include("../action_save.php");
$tmp_name = $_FILES["file"]["tmp_name"];
$name  = $_FILES["file"]["name"];
$nname=explode('.',$name);
if(end($nname)!=="xls"&&end($nname)!=="xlsx"){
echo "<script language=JavaScript>\r\n";
echo "alert('导入文件格式不正确!');\r\n";
echo "location.href='". $_SERVER["HTTP_REFERER"]."'\r\n";
echo "</script>";
}else{
include('../PHPexcel/PHPExcel.php');
date_default_timezone_set('PRC');
$file = iconv("utf-8", "gb2312", $tmp_name);   //转码 
    if(empty($tmp_name) OR !file_exists($tmp_name)) { 
        die('file not exists!'); 
    } 
$objRead = new PHPExcel_Reader_Excel2007();   //建立reader对象 
    if(!$objRead->canRead($tmp_name)){ 
        $objRead = new PHPExcel_Reader_Excel5(); 
        if(!$objRead->canRead($tmp_name)){ 
            die('No Excel!'); 
        } 
    } 
$cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ'); 
$obj = $objRead->load($file);
$currSheet = $obj->getSheet(0);   //获取指定的sheet表 
    $columnH = $currSheet->getHighestColumn();   //取得最大的列号 
    $columnCnt = array_search($columnH, $cellName); 
    $rowCnt = $currSheet->getHighestRow();   //获取总行数 
    function tostr($str){
    	if($cellValue instanceof PHPExcel_RichText){   //富文本转换字符串 
                $cellValue = $str->__toString(); 
            }else{
            	$cellValue = $str;
            }
            $cellValue =str_replace("'","''",$cellValue);
            return $cellValue;
    }
for($_row=2; $_row<=$rowCnt; $_row++){  //读取内容 
            $xs_name = $currSheet->getCell($cellName[0].$_row)->getValue(); 
            $shenfenzheng = $currSheet->getCell($cellName[1].$_row)->getValue(); 
            $danwei = $currSheet->getCell($cellName[2].$_row)->getValue(); 
            $lxr = $currSheet->getCell($cellName[3].$_row)->getValue(); 
            $lxr_tel = $currSheet->getCell($cellName[4].$_row)->getValue(); 
            $email = $currSheet->getCell($cellName[5].$_row)->getValue(); 
            $tel = $currSheet->getCell($cellName[6].$_row)->getValue(); 
            $sex = get_xingbie($shenfenzheng);
            $age = ageVerification($shenfenzheng);
            $result=mysql_query("select * from ph_xuesheng where shenfenzheng='".tostr($shenfenzheng)."'");
            if(mysql_num_rows($result)>=1){
            	echo tostr($shenfenzheng).' 重复<br/>';
            }else{
            	mysql_query("INSERT INTO ph_xuesheng(xs_name,xs_number,shenfenzheng,lxr,lxr_tel,sex,age,tel,danwei,email)VALUES('".tostr($xs_name)."','".tostr($xs_number)."','".tostr($shenfenzheng)."','".tostr($lxr)."','".tostr($lxr_tel)."','".tostr($sex)."','".tostr($age)."','".$tel."','".tostr($danwei)."','".tostr($email)."')",$conn);
            }
    } 
insertaction($_SESSION["managername"],"批量导入用户表".$name,$conn);
echo "<script language=JavaScript>\r\n";
echo "alert('导入完成!');\r\n";
// echo "location.href='list.php'\r\n";
echo "</script>";
}
?>
<div style="width: 100%;height: 100px;line-height: 100px;text-align: center;"><a href="list.php">返回列表</a></div>

